﻿using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class GatherLootTable : LootTable, IInteractable
{
    [SerializeField]
    private Sprite beforeGathering;
    [SerializeField]
    private Sprite afterGathering;
    [SerializeField]
    private SpriteRenderer spriteRenderer;
    [SerializeField]
    private int maxAmount;

    private void Start()
    {
        RollLoot();
    }

    protected override void RollLoot()
    {
        
        MyDroppedItems = new List<Drop>();
        foreach (Loot item in loot)
        {

            int baseAmount = Random.Range(1, maxAmount);
            int luck = Random.Range(0, 100);

            if (luck >= 100 - (item.MyDropChanse / 2))
            {
                for (int i = 0; i < (baseAmount * 2); i++)
                {
                    MyDroppedItems.Add(new Drop(item.MyItem, this));
                }

            }
            else if (luck >= 100 - item.MyDropChanse)
            {
                for (int i = 0; i < baseAmount; i++)
                {
                    MyDroppedItems.Add(new Drop(item.MyItem, this));
                }
            }
            else
            {
                gameObject.SetActive(false);
            }
            gotRolled = true;
        }
    }

    public void Interact()
    {
        Player.MyInstance.Gather(skillBook.MyInstance.getSkill("Herbalism"), MyDroppedItems);
        LootWindow.MyInstance.MyInteractable = this;
    }

    public void StopInteract()
    {
        LootWindow.MyInstance.MyInteractable = null;
        if (MyDroppedItems.Count == 0)
        {
            spriteRenderer.sprite = afterGathering;
            gameObject.SetActive(false);
        }
        LootWindow.MyInstance.Close();
    }
}